package com.adam.framework.util;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

public class BeanUtilTestCase extends TestCase {
	private Car car = null;

	/**
	 * Set up instance variables required by this test case.
	 */
	public void setUp() {
		car = new Car();
	}
	/**
	 * Test narrowing and widening conversions on double.
	 */
	@SuppressWarnings("unchecked")
	public void testPopulate() throws Exception {
		BeanUtil beanUtil = BeanUtil.getInstance();
		Map properties = new HashMap<String, String>();
		properties.put("doubleProperty", "10.257");
		properties.put("booleanProperty", "true");
		properties.put("byteProperty", "121");

		// array
		String[] array = { "123", "456", "789" };
		properties.put("intArray", array);

		beanUtil.populate(car, properties);
		assertEquals((byte) 121, car.getByteProperty());
		assertEquals(10.257D, car.getDoubleProperty());
		assertEquals(true, car.getBooleanProperty());

		int[] intArray = car.getIntArray();
		assertNotNull("intArray is present", intArray);
		assertEquals("intArray length", 3, intArray.length);
		assertEquals("intArray[0]", 123, intArray[0]);
		assertEquals("intArray[1]", 456, intArray[1]);
		assertEquals("intArray[2]", 789, intArray[2]);
	}	
	@SuppressWarnings("unchecked")
	public void testNestedPopulate() throws IllegalAccessException, InvocationTargetException{
		BeanUtil beanUtil = BeanUtil.getInstance();
		Map properties = new HashMap<String, String>();
		properties.put("wheel.tyre.color", "black");

		beanUtil.populate(car, properties);
		assertEquals("black", car.getWheel().getTyre().getColor());
	}
}